package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.threadlocal.WmThreadLocal;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        try {

        //0.重新定义文件名称
        String originalFilename = file.getOriginalFilename();//获取原始名称
        //截取后缀名
        int i = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(i);
        String fileName= UUID.randomUUID().toString()+suffix;
        //1.上传图片到minio中，返回路径地址
        String path = fileStorageService.uploadImgFile("", fileName, file.getInputStream());
        //2.新增wm_material表

            WmMaterial wmMaterial=new WmMaterial();
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setType((short) 0);
            wmMaterial.setUrl(path);
            wmMaterial.setUserId(WmThreadLocal.getWmUser().getId());
            save(wmMaterial);

            //3.返回数据
            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.UPLOAD_FAILE);
        }
    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1.判断参数
        dto.checkParam();

        //2.isCollection=0,全查，=1根据是否收藏来作为条件
        LambdaQueryWrapper<WmMaterial> queryWrapper=new LambdaQueryWrapper<>();
        if(dto.getIsCollection()!=0){
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //3.执行分页查询
        IPage ipage=new Page(dto.getPage(),dto.getSize());

        ipage = page(ipage, queryWrapper);


        //4.封装数据
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),Integer.parseInt(ipage.getTotal()+""));
        //封装查询列表数据
        result.setData(ipage.getRecords());

        return result;
    }
}
